import Vue from "vue";
import { login, getInfo, logout } from "@/api/login";
import { ACCESS_TOKEN, TOKEN_PREFIX } from "@/store/mutation-types";
import { welcome } from "@/utils/util";

const user = {
    state: {
        token: "",
        name: "",
        welcome: "",
        avatar: "",
        menus: [],
        menusMap: new Map(),
        roles: [],
        permissions: [],
        info: {}
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token;
            console.log("--------------------设置后token的state-------：",state)
        },
        SET_NAME: (state, { name, welcome }) => {
            state.name = name;
            state.welcome = welcome;
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar;
        },
        SET_MENUS: (state, menus) => {
            state.menus = menus;
        },
        SET_MENUS_MAP: (state, menusMap) => {
            state.menusMap = menusMap;
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles;
        },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = permissions;
        },
        SET_INFO: (state, info) => {
            state.info = info;
        }
    },

    actions: {
        // 登录
        Login ({ commit }, userInfo) {
            return new Promise((resolve, reject) => {
                login(userInfo).then(response => {
                    // console.log("登录接口返回：",response);
                    const token = response.headers[ACCESS_TOKEN.toLowerCase()];
                    if (!token) {
                        reject(new Error("登录失败!"));
                    } else {
                        // Vue.ls.set(ACCESS_TOKEN, token, 7 * 24 * 30 * 60 * 1000);
                        commit("SET_TOKEN", token);
                        resolve();
                    }
                }).catch(error => {
                    reject(error);
                });
            });
        },

        // 获取用户信息
        GetInfo ({ commit }) {
            return new Promise((resolve, reject) => {
                getInfo().then(response => {
                    console.log("获取用户信息接口", response);
                    const result = response.data;

                    // if (result.roles && result.roles.length > 0) {
                    // const role = result.role;
                    // role.permissions = result.role.permissions;
                    // role.permissions.map(per => {
                    //   if (
                    //     per.actionEntitySet != null &&
                    //     per.actionEntitySet.length > 0
                    //   ) {
                    //     const action = per.actionEntitySet.map(action => {
                    //       return action.action;
                    //     });
                    //     per.actionList = action;
                    //   }
                    // });
                    // role.permissionList = role.permissions.map(permission => {
                    //   return permission.permissionId;
                    // });
                    commit("SET_ROLES", result.roles);
                    commit("SET_PERMISSIONS", result.permissions);
                    commit("SET_INFO", result);
                    // } else {
                    //   reject(new Error("角色不能是空集合"));
                    // }



                    commit("SET_NAME", { name: result.name, welcome: welcome() });
                    commit("SET_AVATAR", result.avatar);
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            });
        },

        // 登出
        Logout ({ commit, state }) {

            return new Promise(resolve => {
                logout(state.token)
                    .then(() => {
                        console.log("前端调用后端登出接口成功，正常登出")
                        // resolve();
                        // Vue.ls.remove(ACCESS_TOKEN);
                        // commit("SET_TOKEN", "");
                        // commit("SET_ROLES", []);
                        // commit("SET_PERMISSIONS", []);
                        // commit("SET_MENUS", []);
                        // commit("SET_INFO", []);
                    })
                    .catch(() => {
                        console.log("前端调用后端登出接口失败，异常登出")
                        // resolve();
                        // Vue.ls.remove(ACCESS_TOKEN);
                        // commit("SET_TOKEN", "");
                        // commit("SET_ROLES", []);
                        // commit("SET_PERMISSIONS", []);
                        // commit("SET_MENUS", []);
                        // commit("SET_INFO", []);
                    })
                .finally(() => {
                        resolve();
                        console.log("..............................前端调用后端登出接口：finally 执行.................................................")
                        Vue.ls.remove(ACCESS_TOKEN);
                        commit("SET_TOKEN", "");
                        commit("SET_ROLES", []);
                        commit("SET_PERMISSIONS", []);
                        commit("SET_MENUS", []);
                        commit("SET_INFO", []);
                });


            });
        }
    }
};

export default user;
